package com.wangjiyuan.giftshuo.ui.fragment.other.search;


import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.adapter.HistoryAdapter;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.search.HistoryBean;
import com.wangjiyuan.giftshuo.bean.search.HistoryBeanDao;
import com.wangjiyuan.giftshuo.widget.FlowLayout;

import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;

import static com.wangjiyuan.giftshuo.base.App.app;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends BaseFragment implements View.OnClickListener {
    private HistoryAdapter adapter;
    private EditText search;
    private TextView cancel;
    private ImageView back, clear;
    private FlowLayout hot_word_content;
    private ListView history;
    private List<HistoryBean> list = new ArrayList<>();

    public SearchFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        history = (ListView) view.findViewById(R.id.history);
        hot_word_content = (FlowLayout) view.findViewById(R.id.hot_word_content);
        search = (EditText) view.findViewById(R.id.search);
        cancel = (TextView) view.findViewById(R.id.cancel);
        back = (ImageView) view.findViewById(R.id.back);
        clear = (ImageView) view.findViewById(R.id.clear);
        cancel.setOnClickListener(this);
        back.setOnClickListener(this);
        clear.setOnClickListener(this);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("")) {
                    clear.setVisibility(View.INVISIBLE);
                } else {
                    clear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();

        List<String> hot = (List<String>) bundle.getSerializable("hot");
        for (int i = 0; i < hot.size(); ++i) {
            TextView tv = new TextView(mContext);
            float density = getResources().getDisplayMetrics().density;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins((int) (5 * density + 0.5f), (int) (5 * density + 0.5f), (int) (5 * density + 0.5f), (int) (5 * density + 0.5f));
            tv.setLayoutParams(params);
            tv.setBackgroundColor(getResources().getColor(R.color.mpink));
            tv.setGravity(Gravity.CENTER);
            tv.setPadding((int) (15 * density + 0.5f), (int) (5 * density + 0.5f), (int) (15 * density + 0.5f), (int) (5 * density + 0.5f));
            tv.setText(hot.get(i));
            if (i < 3) {
                tv.setTextColor(Color.RED);
            }
            tv.setTextSize(16);
            hot_word_content.addView(tv);
        }
        HistoryBeanDao historyBeanDao = app.getDaoSession().getHistoryBeanDao();
        Database db = app.getDb();

//        list.addAll(historyBeanDao.queryRaw("where _id=?",
//                new String[]{"*"}));
        Cursor cursor = db.rawQuery("select * from search_history", null);
        while (cursor.moveToNext()) {
            HistoryBean bean = new HistoryBean();
            bean.setId(cursor.getLong(cursor.getColumnIndex("_id")));
            bean.setContent(cursor.getString(cursor.getColumnIndex("CONTENT")));
            list.add(bean);
        }
        if (list.size() == 0) {
            history.setVisibility(View.INVISIBLE);
        }
        adapter = new HistoryAdapter(mContext, list);
        adapter.setListener(new HistoryAdapter.onListIsEmpty() {
            @Override
            public void onListIsEmpty() {
                history.setVisibility(View.INVISIBLE);
            }
        });
        history.setAdapter(adapter);
        history.addHeaderView(initHead());
    }

    private View initHead() {
        View inflate = getActivity().getLayoutInflater().inflate(R.layout.item_head, null);
        ImageView delete_all = (ImageView) inflate.findViewById(R.id.delete);
        delete_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                app.getDaoSession().deleteAll(HistoryBean.class);
                history.setVisibility(View.INVISIBLE);
            }
        });

        return inflate;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
            case R.id.back:
                getActivity().getSupportFragmentManager()
                        .beginTransaction().remove(this).commit();
                break;
            case R.id.clear:
                search.setText("");
                break;
        }
    }
}
